package com.etc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/DownLoadController")
public class DownLoadController {

    /**
     * 文件下载
     * @param request
     * @param response
     * @param filename
     * @throws IOException
     */
    @RequestMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response,String filename) throws IOException {
        //*告诉客户端这个文件不是解析 而是以附件的形式下载
        response.setHeader("Content-Disposition", "attachment;filename="+filename);
        //2.获取文件的绝对路径
        String path="D:\\uploadFile\\"+filename;
        //3.获得该文件的输入流
        InputStream in=new FileInputStream(path);
        //获得输出流---通过response获得的输出流  用于向客户端写内容
        ServletOutputStream out=response.getOutputStream();
        //拷贝文件
        int len=0;
        byte [] buffer=new byte[1024];

        while((len=in.read(buffer))>0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
    }

}
